define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'feditionaccountpay/index' + location.search,
                    add_url: 'feditionaccountpay/add',
                    edit_url: 'feditionaccountpay/edit',
                    del_url: 'feditionaccountpay/del',
                    multi_url: 'feditionaccountpay/multi',
                    import_url: 'feditionaccountpay/import',
                    table: 'fedition_accountpay',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        // {field: 'account_address', title: __('绑定店铺名称'),visible: false,addClass: "selectpage", extend: "data-source='feditionaccounturl/index' data-field='name'"},
                        // {field: 'account_address_name', title: __('绑定店铺名称'), operate: false},
                        {field: 'collection_type', title: __('Collection_type'),visible: false,addClass: "selectpage", extend: "data-source='feditioncollectiontype/index' data-field='name'"},
                        {field: 'collection_type_name', title: __('Collection_type'), operate: false},
                        {field: 'balance', title: __('Balance'), operate:'BETWEEN'},
                        {field: 'virtualname', title: __('Virtualname'), operate: 'LIKE'},
                        {field: 'virtualpsd', title: __('Virtualpsd'), operate: 'LIKE'},
                        {field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'emailpsd', title: __('Emailpsd'), operate: 'LIKE'},
                        {field: 'remarks', title: __('Remarks'), operate: 'LIKE'},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('关闭'),"examine":__('审核中')}, formatter: Table.api.formatter.status},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('店铺收款'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('关联店铺'),
                                    title: __('关联店铺'),
                                    classname: 'btn btn-sm btn-warning btn-dialog',
                                    icon: 'fa fa-folder-o',
                                    url: 'feditionaccounturl/index/?accountpay={ids}'
                                },
                                {
                                    name: 'detail',
                                    text: __('店铺收款明细'),
                                    title: __('店铺收款明细'),
                                    extend: 'data-area=\'["90%","90%"]\'',
                                    classname: 'btn btn-sm btn-success btn-dialog',
                                    icon: 'fa fa-magic',
                                    url: 'feditionaccountpaystore/index'
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            $(document).on("click", ".btn-collectiontype", function () {
                Fast.api.open("feditioncollectiontype/index", __('收款账户类型'));
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                require(['selectpage'], function (SelectPage) {
                    var _displayResults = $.fn.selectPage.Constructor.prototype.displayResults;
                    $.fn.selectPage.Constructor.prototype.displayResults = function (self, json, is_query) {
                        _displayResults.call(this, self, json, is_query);
                        if (self.elem.hidden.is("#c-collection_type")) {
                            $(self.elem.results[0]).append("<p class='text-center' style='margin:5px;'><a class='btn btn-xs btn-info btn-dialog' title='新增收款账户类型' data-window='top' href='feditioncollectiontype/add'>新增收款账户类型</a></p><p style='margin:5px;'>（新增收款账户类型后需要取消下拉框，重新选择）</p>");
                        }
                    };
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});